package org.example;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

public class AppReadSimpleJar2 {
    public static void main(String[] args) throws Exception {
        HashMap<String, byte[]> map = new HashMap<>();

        String jarPath = System.getProperty("user.dir") + "/jarFileRear/jarFileDir/jsqlParser-1.0-SNAPSHOT.jar";
        File jarFilePath = new File(jarPath);

        JarFile jarFile = new JarFile(jarFilePath);

        //读取jar包中的内容
        ZipEntry myPropertiesEntry = jarFile.getEntry("my.properties");
        InputStream myPropertiesInputStream = jarFile.getInputStream(myPropertiesEntry);
        //流对象是:java.util.zip.ZipFile$ZipFileInputStream
        System.out.println("流对象是:"+myPropertiesInputStream.getClass().getName());
        System.out.println(myPropertiesEntry.getSize());

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();


//        Properties properties = new Properties();
//        properties.load(myPropertiesInputStream);
//        System.out.println("properties----加载内容----"+properties);


        int len = -2;
        byte[] buffer = new byte[10];
        while ( (len = myPropertiesInputStream.read(buffer)) != -1 ) {
            System.out.println("-------------------进入了");
            byteArrayOutputStream.write(buffer, 0, len);
        }

        byte[] byteArray = byteArrayOutputStream.toByteArray();
//        System.out.println(byteArray[13]);
//        System.out.println(byteArray[14]);

        String s = new String(byteArray);
        System.out.println(s);


        Manifest manifest = jarFile.getManifest();

        System.out.println(manifest);

        Map<String, Attributes> entries = manifest.getEntries();
        System.out.println("entries-----"+entries  );

        Attributes mainAttributes = manifest.getMainAttributes();

        Set<Map.Entry<Object, Object>> entries1 = mainAttributes.entrySet();
        System.out.println(entries1);

        jarFile.close();

    }
}
